﻿using ExcelSheetOperateion;
using PrjAssess.Models;
using PrjAssess.Models.Service.AssessProject;
using PrjAssessModels;
using PrjAssessModels.BaseModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace PrjAssess.Controllers
{
    public class ReadAssessController : Controller 
    {
        //
        // GET: /ReadAssess/

        public ActionResult Index(string err = "")
        {
            ViewBag.Err = err;
            return View();
        }






        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UploadAssessSheet()
        {
            if (Request.Files.Count > 0)
            {
                var file = Request.Files[0];
                Project prj =  AssessProject.ReadUpload(file);
                if (prj != null)
                {
                    TempData["prj"] = prj;
                    return RedirectToAction("Display", "ReadAssess");

                }
                else
                {
                    return RedirectToAction("Index", "ReadAssess", new { err = "上传发生错误!" });
                }
              
            }
            return RedirectToAction("Index", "ReadAssess", new { err = "上传过程发成错误" });
        }




        #region Display Basic Project Information
        [AcceptVerbs(HttpVerbs.Post)]
        [HttpPost]
        public JsonResult SaveAssess(string prjJson)
        {
            Project prj = Newtonsoft.Json.JsonConvert.DeserializeObject<Project>(prjJson);
            TempData["prj"] = prj;
            return Json("1");
        }

        //当前的工程
        public ActionResult DisplayCurPrj()
        {
            Project prj = ProjectDataHelper.GetByPrjId(GlobalData.PrjId);
            prj.FromExcel = false;
            TempData["prj"] = prj;
            return RedirectToAction("Display");
        }
        public ActionResult FillInBlank()
        {
            TempData["prj"] = null;
            return RedirectToAction("Display");
        }
        //显示工程测算信息
        public ActionResult Display()
        {
            if (TempData["prj"] != null)
            {
                return View(TempData["prj"] as Project);
            }
            else
            {
                Project prj = new Project();
                prj.FromExcel = false;
                //this is a new Assess sheet
               
                return View(prj);
            }
            
        }
        #endregion


        #region Display meterial and bury items
        public ActionResult DisplayMertialBury()
        {
            Project prj = TempData["prj"] as Project;
            if (prj != null)
            {
                if (prj.FromExcel == false)
                {
                    // to do
                    //如果不是来自excel的测算信息,应该根据测算的时间,
                    //对测算的材料和埋设的单价进行赋值
                }
                return View(prj);
            }
            else
            {
                return View(new Project());
            }
        }
        #endregion

        #region Display  Work Items
        public ActionResult DisplayWorks()
        {
            if (TempData["prj"] != null)
            {
                Project prj = TempData["prj"] as Project;
                if (prj.FromExcel == false)
                {
                    //to do
                    //generate Work Items according to Material and Bury Items
                    //assign the matched price from DataBase based on Assess Date (2000-1-1 to 2013-9-1)

                }
                return View(prj);
            }
            else
            {
                return View(new Project());
            }
        }
        //[HttpPost]
        //public JsonResult NeedReAssess(string prjId)
        //{
        //    var result = new { Success = false, DirectlySave = false, NeedReAssess = false, Msg = "" };
        //    if (Request.Files.Count > 0)
        //    {
        //        var file = Request.Files[0];
        //        Project prj = AssessProject.ReadUpload(file);
        //        if (prj != null)
        //        {
        //            string msg; bool need;
        //            if (AssessProject.CanDirectlySaveProject(prj, out need, out msg))
        //            {
        //                result = new { Success = true, DirectlySave = true, NeedReAssess = false, Msg = "" };
        //            }
        //            else
        //            {
        //                result = new { Success = true, DirectlySave = false, NeedReAssess = need, Msg = msg };
        //            }
        //        }
        //    }
        //    return this.Json(result);
        //}

        [HttpPost]
        public JsonResult CanDirectlySaveAssess(string prjJson)
        {
            Project prj = Newtonsoft.Json.JsonConvert.DeserializeObject<Project>(prjJson);
            bool need;
            string msg;
            //true means Prj will be Directly saved into database
            //false means Prj Existed
            bool IsSaved = AssessProject.CanDirectlySaveProject(prj, out need, out msg);
            TempData["prj"] = prj;
            dynamic result = new { IsSave = IsSaved, NeedReSave = need, Msg = msg };
            return Json(result);
        }
        [HttpPost]
        public JsonResult SaveAssessIntoDataBase(string prjJson)
        {
            Project prj = Newtonsoft.Json.JsonConvert.DeserializeObject<Project>(prjJson);
            if (ProjectDataHelper.InsertProject(prj))
            {
                return Json("1");
            }
            else
            {
                return Json("0");
            }

        }
        #endregion

    }
}
